package com.eliza.music.view.music.splash

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.eliza.music.model.vm.VmSplash
import com.eliza.music.ui.theme.FontSize
import com.eliza.music.ui.theme.PaddingSize
import com.eliza.music.R

@Preview
@Composable
private fun SplashPreview() {
    Splash.Route()
}

object Splash {

    @Composable
    fun Route() {
        Screen()
    }

    @Composable
    fun Screen(vm: VmSplash = hiltViewModel()) {
        val sleepDuration by vm.sleepDuration.collectAsStateWithLifecycle()
        val nextToMain by vm.nextToMain.collectAsStateWithLifecycle()

        if (nextToMain) {
            LaunchedEffect(nextToMain) {
                Log.e("TAG", "Splash Screen: ${nextToMain}")
            }
        }
        Box() {

            Image(
                painter = painterResource(id = R.drawable.splash),
                contentDescription = null,
                contentScale = ContentScale.Fit,
                modifier = Modifier.fillMaxSize()
            )
            Text(
                "跳过 ${sleepDuration} s",
                fontSize = FontSize.Large.size,
                fontWeight = FontWeight.Bold,
                color = Color.LightGray,
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .padding(PaddingSize.Large.size)
                    .background(Color.Black.copy(0.3f), RoundedCornerShape(20))
                    .clickable {
                        vm.onClick()
                    }
                    .padding(PaddingSize.Small.size)

            )

        }
    }


}

